#include <stdio.h>

void find(int *a, int n, int *i, int *j, int *k)
{
    int m, t1, t2, t3;
    
    t1 = t2 = t3 = 0;
    for (m = 0; m < n; m++) {
        if (t1 == 0) {
            *i = a[m];
            t1 ++;
        }
        else if (a[m] == *i)
            t1 ++;
        else if (t2 == 0) {
            *j = a[m];
            t2 ++;
        }
        else if (a[m] == *j)
            t2 ++;
        else if (t3 == 0) {
            *k = a[m];
            t3 ++;
        }
        else if (a[m] == *k)
            t3 ++;
        else {
            t1 --;
            t2 --;
            t3 --;
        }
    }
}

int main(int argc, char *argv[])
{
    int a[] = {1, 2, 3, 4, 3, 6, 1, 3, 1, 2, 2, 8, 2, 1, 3};
    int i, j, k;

    find(a, (sizeof a)/(sizeof (int)), &i, &j, &k);

    printf("three water king is %d, %d, %d\n", i, j, k);

    return 0;
}
